package com.gg.registration.user.api;

import com.gg.common.result.CommonResult;
import com.gg.common.result.ResultCodeEnum;
import com.gg.common.utils.AuthContextUtils;
import com.gg.registration.model.user.Patient;
import com.gg.registration.user.service.PatientService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Author：  chenjunjia
 * Date：    2022/4/21 18:56
 * WeChat：  China_JoJo_
 */
@RestController
@RequestMapping("/api/user/patient")
public class PatientController {

    @Resource
    PatientService patientService;

    //查询就诊人列表
    @GetMapping("auth/findAll")
    public CommonResult findAll(HttpServletRequest request) {
        Long userId = AuthContextUtils.getUserId(request);
        List<Patient> patientList = patientService.findAll(userId);
        return patientList==null? new CommonResult(ResultCodeEnum.FAIL) : new CommonResult(ResultCodeEnum.SUCCESS,patientList);
    }

    //添加就诊人
    @PostMapping("auth/save")
    public CommonResult savePatient(@RequestBody Patient patient, HttpServletRequest request) {
        //获取当前登录用户id
        Long userId = AuthContextUtils.getUserId(request);
        patient.setUserId(userId);
        patientService.save(patient);
        return new CommonResult(ResultCodeEnum.SUCCESS);
    }

    /**
     * 根据就诊人id查询就诊人详细信息
     * @param id 就诊人id
     * @return
     */
    @GetMapping("auth/get/{id}")
    public CommonResult getPatient(@PathVariable Long id) {
        Patient patient = patientService.getPatientId(id);
        return new CommonResult(ResultCodeEnum.SUCCESS,patient);
    }

    //修改就诊人
    @PostMapping("auth/update")
    public CommonResult updatePatient(@RequestBody Patient patient) {
        patientService.updateById(patient);
        return new CommonResult(ResultCodeEnum.SUCCESS);
    }

    //删除就诊人
    @DeleteMapping("auth/remove/{id}")
    public CommonResult removePatient(@PathVariable Long id) {
        patientService.removeById(id);
        return new CommonResult(ResultCodeEnum.SUCCESS);
    }

    /**
     * 订单调用：获取就诊人信息
     * @param id
     * @return
     */
    @GetMapping("inner/get/{id}")
    public Patient getPatientOrder(@PathVariable("id") Long id) {
        return patientService.getById(id);
    }
}
